" 创建分屏布局
function! trainer#ui#create_layout() abort
  " 保存当前窗口
  let s:prev_window = winnr()
  
  " 创建右侧信息窗口 (40% 宽度)
  vertical botright 40vnew 
  
  " 设置信息窗口属性
  setlocal buftype=nofile
  setlocal bufhidden=wipe
  setlocal noswapfile
  setlocal nonumber
  setlocal norelativenumber
  setlocal nowrap
  setlocal winfixwidth
  setlocal modifiable
  
  " 初始内容
  call setline(1, [
        \ '=== Vim Trainer ===',
        \ '',
        \ 'Welcome to the Vim Trainer plugin!',
        \ '',
        \ 'This is the information panel.',
        \ 'It will display helpful information',
        \ 'as you use the plugin.',
        \ '',
        \ '--- Layout Info ---',
        \ 'Left:  Editing area',
        \ 'Right: Information area',
        \ '',
        \ '--- Commands ---',
        \ ':TrainerStart    - Start trainer',
        \ ':q               - Close trainer'
        \ ])
  
  " 设置只读
  setlocal nomodifiable
  silent! file `='[Trainer Info]'`
  
  " 保存缓冲区编号
  let g:trainer_info_bufnr = bufnr('%')
  
  " 返回编辑窗口
  execute s:prev_window . 'wincmd w'
  
  " 设置编辑窗口属性
  setlocal buftype=acwrite
  setlocal bufhidden=hide
  setlocal noswapfile
  setlocal modifiable
  setlocal noreadonly

  let g:trainer_edit_bufnr = bufnr('%')

      python3 << EOF
import vim
import trainer.manager as tm
try:
    edit_bufnr = int(vim.eval('g:trainer_edit_bufnr'))
    info_bufnr = int(vim.eval('g:trainer_info_bufnr'))
    # 将窗口标识传给python
    tm.manager.init_renderer(edit_bufnr, info_bufnr)
except Exception as e:
    vim.command('echo "Failed to init layout: %s"' % e)
EOF
endfunction

" 更新信息区域
function! trainer#ui#update_info(message) abort
  " 查找信息窗口
  let info_win = bufwinnr('[Trainer Info]')
  if info_win == -1
    return
  endif
  
  " 切换到信息窗口
  execute info_win . 'wincmd w'
  setlocal modifiable
  
  " 在末尾添加新消息
  let new_line = '[' . strftime('%H:%M:%S') . '] ' . a:message
  call append(line('$'), new_line)
  
  " 滚动到底部
  normal! G
  setlocal nomodifiable
  
  " 返回编辑窗口
  wincmd p
endfunction